﻿package little.bunny;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;


public class MyHome extends Activity {
	private final String serviceNameSpace = "http://littlebunny.com/";
	private final String method_name = "CustomLogin";
	private final String serviceUrl = "http://www.littlebunny.com.cn/bunny_service/bunny_service.asmx";
	private final String SOAP_ACTION = "http://littlebunny.com/CustomLogin";
	
	private int[] btids = {R.id.btn_viewjifen,R.id.btn_viewxiaofei,R.id.btn_viewduihuan};
    private Button[] bt = new Button[btids.length]; 
    private ImageView logoImageView;
    private Button backButton;
    private Button btn_bind;
    private Button btn_update;
    private TextView textUserCode;
    private String Card_ID;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_bunny);
        
        SharedPreferences sharedata2 = getSharedPreferences("user_data", 0);  
		String s_user = sharedata2.getString("name", null);
		
        textUserCode = (TextView) findViewById(R.id.txtUser_Name);
        textUserCode.setText(s_user);
		
        Card_ID = "";
        
        //退出按钮
        backButton=(Button)findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				//finish();
				Intent myIntent=new Intent(MyHome.this,XiaoBaiTuActivity.class);
				startActivity(myIntent);
			}
		});
        
		//读取用户会员卡信息
		read_card_list();
		
		//绑定会员卡
		btn_bind=(Button)findViewById(R.id.btn_bind_card);
		btn_bind.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent myIntent=new Intent();
				myIntent.setClass(MyHome.this, MyBindCard.class);
				startActivity(myIntent);
			}
		});
		
		//更新会员信息 
		btn_update=(Button)findViewById(R.id.btn_udpate_data);
		btn_update.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent myIntent=new Intent();
				myIntent.setClass(MyHome.this, MyAddress.class);
				startActivity(myIntent);
			}
		});
		
		//头部的标题
        logoImageView=(ImageView)findViewById(R.id.logo_imageView);
        logoImageView.setFocusable(true);
        logoImageView.setFocusableInTouchMode(true);
        
        //按钮绑定事件
        searchBtAndListener();
                
    }
    
    //获取按钮，添加事件
    private void searchBtAndListener(){
    	for (int i = 0; i < btids.length; i++) {
			bt[i]=(Button)findViewById(btids[i]);
			bt[i].setOnClickListener(new btOnChilkListener());
		}
    }
    
    //事件监听器 
    private class btOnChilkListener implements OnClickListener{

		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent myIntent=new Intent();
			
			//把用户名传给下一个activity
			Bundle bdle = new Bundle(); 
			bdle.putString("KEY_CARD",Card_ID);
			myIntent.putExtras(bdle);
			
			//积分查询
			if (v==bt[0])
			{
				myIntent.setClass(MyHome.this, CustomJiFen.class);
				startActivity(myIntent);
			}
			//消费记录
			if (v==bt[1])
			{
				myIntent.setClass(MyHome.this, CustomXiaoFei.class);
				startActivity(myIntent);
			}
			//积分兑换
			if (v==bt[2])
			{
				AlertDialog alert = new AlertDialog.Builder(MyHome.this).create();
				alert.setTitle("温馨提示");
				alert.setMessage("积分兑换礼品稍后推出");
				Message msg=null;
				alert.setButton("确定", msg);
				//alert.PositiveButton("", null);
				alert.show();
			}
			
		}
    }
    
    private void read_card_list(){
    	Thread networkThread = new Thread() {
			@Override
			public void run() {
				SharedPreferences sharedata = getSharedPreferences("user_data", 0);  
				
				String s_user = sharedata.getString("name", null);
				String s_pass = sharedata.getString("pass_key", null);
				
				SoapObject request = new SoapObject(serviceNameSpace, method_name);
				request.addProperty("user_id", s_user);
				request.addProperty("u_pass", s_pass);

				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.dotNet = true;
				envelope.setOutputSoapObject(request);
				
				try {
					HttpTransportSE ht = new HttpTransportSE(serviceUrl);
					ht.call(SOAP_ACTION, envelope);

					final SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
					final String result = response.toString();
					
					runOnUiThread(new Runnable() {
						public void run() {
							if (result.startsWith("99:", 0))
							{
								ShowMyDialog("用户登录",result.substring(3));
							}
							else
							{
								if (result.length()==0)
								{
									ShowMyDialog("用户登录","网络错误");
								}
								
								ShowCardList(result.substring(3));
							}
						}
					});
				} catch (Exception e) {
					e.printStackTrace();
					ShowMyDialog("网络信号异常","请查看网络连接，稍候再试...");
					
					//Toast.makeText(this, "网络错误,请检查能否访问网络", Toast.LENGTH_LONG).show();
					//TextView txt1;
					//txt1 = (TextView) findViewById(R.id.textShowJiFen);
					//txt1.setText("网络错误,请检查能否访问网络");
				}
			}
		};

		networkThread.start();
    }
    
    private void ShowMyDialog(String title, String info)
	{
		new AlertDialog.Builder(MyHome.this)
		.setTitle(title)
		.setMessage(info)
		.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int which) {
						//MyLogin.this.finish();
					}
				}).show();
	}
    
    //会员卡列表
    private void ShowCardList(String CardNos)
    {
    	if (CardNos.length()==0)
    	{
    		return;
    	}
    	
        RadioGroup radiogroup1;
        radiogroup1=(RadioGroup)findViewById(R.id.radiogroup1);
        String[] list=CardNos.split(",");
        
        if(list.length>0){
        	//rgpOrder=new RadioGroup(this);
            for (int i = 0; i < list.length; i++) {
                RadioButton rdoAddress= new RadioButton(this);
                rdoAddress.setId(i); //控件id一定要有
                rdoAddress.setTextColor(Color.BLACK);
                rdoAddress.setTextSize(14);
                rdoAddress.setText(list[i]);
                if (i==0)
                	rdoAddress.setChecked(true);
                
                radiogroup1.addView(rdoAddress);
            }
            
            //lytRadioAddress.addView(rgpOrder);
            //绑定RadioGroup的选择改变事件监听器
            radiogroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            	
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton tempButton = (RadioButton)findViewById(checkedId);
                    Card_ID = tempButton.getText().toString();
                    Toast.makeText(MyHome.this, "您选择的是："+tempButton.getText(),
                            Toast.LENGTH_SHORT).show();                    
                }
            });
        }
    }
}
